package com.LachesismStorm.lachesism.member.service.impl;

import com.LachesismStorm.lachesism.core.common.response.ResponsePage;
import com.LachesismStorm.lachesism.core.service.RedisService;
import com.LachesismStorm.lachesism.member.mapper.TftMapper;
import com.LachesismStorm.lachesism.member.service.TftControllerService;
import com.LachesismStorm.lachesism.member.vo.TftVO.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author lachesism
 * @Date 2024-06-05 01:42
 **/
@Service
public class TftControllerServiceImpl implements TftControllerService {

    @Resource
    private TftMapper tftMapper;
    @Resource
    private RedisService redisService;

    @Override
    public ResponsePage<Lineup> getLineupList(Integer pageNum, Integer season) {
        String cacheKey = String.format("lineup_%d", season);
        if (pageNum.equals(1)) {
            List<Lineup> lineup = (List<Lineup>) redisService.getWithTimeout(cacheKey + season);
            if (lineup != null && !lineup.isEmpty()) {
                return ResponsePage.restPage(new PageInfo<>(lineup));
            }
        }
        PageHelper.startPage(pageNum, 12);
        List<Lineup> lineupList = tftMapper.getLineupList(season);
        if (CollectionUtils.isEmpty(lineupList)) {
            return ResponsePage.restPage(new PageInfo<>(Collections.emptyList()));
        }
        List<TftFormationSimple> tftFormationInfoList = tftMapper.getTftFormationInfo(lineupList);
        Map<Integer, List<TftFormationSimple>> formationMap = tftFormationInfoList.stream()
                .collect(Collectors.groupingBy(TftFormationSimple::getFormationId));
        for (Lineup lineup : lineupList) {
            lineup.setFormationInfoList(formationMap.get(lineup.getId()));
        }
        if (pageNum.equals(1)) {
            redisService.set(cacheKey, lineupList, 1);
        }
        return ResponsePage.restPage(new PageInfo<>(lineupList));
    }

    @Override
    public Lineup getLineupInfo(Integer id) {
        Lineup lineUp = tftMapper.getLineup(id);
        List<TftCharacterInfo> characterInfos = tftMapper.getCharacterInfo(id);
        lineUp.setCharacterInfoList(characterInfos);
        return lineUp;
    }

    @Override
    public void addLineup(Lineup lineup) {
        tftMapper.createFormationRelation(lineup);
        List<TftFormationInfo> formationInfo = lineup.getFormationInfo();
        for (TftFormationInfo tftFormationInfo : formationInfo) {
            tftFormationInfo.setFormationId(lineup.getId());
        }
        tftMapper.addLineup(formationInfo);
    }

    @Override
    public List<TftCharacterInfo> getAllCharacter(Integer season) {
        return tftMapper.getAllCharacter(season);
    }

    @Override
    public List<TftEquipment> getAllEquipment(Integer season) {
        return tftMapper.getAllEquipment(season);
    }

    @Override
    public List<Lineup> getLineupByHero(Integer season, String hero) {
        List<Lineup> lineupList = tftMapper.getLineUpByHero(season, hero);
        if (lineupList.isEmpty()) {
            return lineupList; // 已经是空列表，无需额外处理
        }
        List<TftFormationSimple> tftFormationInfoList = tftMapper.getTftFormationInfo(lineupList);
        Map<Integer, List<TftFormationSimple>> formationMap = tftFormationInfoList.stream()
                .collect(Collectors.groupingBy(TftFormationSimple::getFormationId));
        for (Lineup lineup : lineupList) {
            lineup.setFormationInfoList(formationMap.get(lineup.getId()));
        }
        return lineupList;
    }
}
